package com.kaesar.leetcode.LeetCode_001_050;

public class LeetCode_050 {
    /**
     * 方法一：递乘/递除
     *
     * @param x
     * @param n
     * @return
     */
    public static double myPow(double x, int n) {
        if (x == 1) {
            return 1;
        }
        double result = x;
        if (n == 0) {
            result = 1;
        } else if (n > 0) {
            for (int i = 1; i < n; i++) {
                result *= x;
            }
        } else {
            n = Math.abs(n);
            result = 1;
            for (int i = 0; i < n; i++) {
                result /= x;
                if (result < Double.MIN_VALUE) {
                    return 0;
                }
            }
        }
        return result;
    }

    /**
     * 方法二：调用库函数
     *
     * @param x
     * @param n
     * @return
     */
    public static double myPow2(double x, int n) {
        return Math.pow(x, n);
    }

    public static void main(String[] args) {
        System.out.println(myPow(2.0000, -48));
    }
}
